﻿using System;
using Microsoft.Practices.ObjectBuilder;
using Microsoft.Practices.Web.UI.WebControls;
using ModulesToolkit.DataContracts;
using ModulesToolkit.DataContracts.Membership;
using System.Web.UI.WebControls;

namespace ModulesToolkit.Membership.Views
{
    public partial class UsersList : Microsoft.Practices.CompositeWeb.Web.UI.Page, IUsersListView
    {
        private UsersListPresenter _presenter;
        private UserDtoSearchCriteria _searchCriteria;
        private RolesSearchCriteria _rolesSearchCriteria;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                this._presenter.OnViewInitialized();
            }
            this._presenter.OnViewLoaded();

            this.reqFullName.ErrorMessage = this.reqFullName.ErrorMessage.Replace("{0}", "Full Name");
            this.regexpName.ErrorMessage = this.regexpName.ErrorMessage.Replace("{0}", "Full Name");
            this.reqUserName.ErrorMessage = this.reqUserName.ErrorMessage.Replace("{0}", "User Name");
            this.reqEmail.ErrorMessage = this.reqEmail.ErrorMessage.Replace("{0}", "E-mail");
        }

        [CreateNew]
        public UsersListPresenter Presenter
        {
            get
            {
                return this._presenter;
            }
            set
            {
                if (value == null)
                    throw new ArgumentNullException("value");

                this._presenter = value;
                this._presenter.View = this;
            }
        }


        public void FillUsersList(EntitySearchResult<UserDto> users)
        {
            this.uxUsersListSource.DataSource = users;
        }

        protected void uxUsersListSource_Selecting(object sender, ObjectContainerDataSourceSelectingEventArgs e)
        {
            this.UsersSearchCriteria.SortField = e.GetSortExpression();
            this.UsersSearchCriteria.SortDirection = e.GetSortDirection();
            this.Presenter.OnSearchClick();
        }


        public UserDtoSearchCriteria UsersSearchCriteria
        {
            get
            {
                if (_searchCriteria == null)
                    _searchCriteria = new UserDtoSearchCriteria();

                _searchCriteria.FullName = this.txtFullName.Text;
                _searchCriteria.Username = this.txtUserName.Text;
                _searchCriteria.Email = this.txtMail.Text;
               

                bool isEnaled = true;
                if (this.uxIsEnabled.SelectedValue.ToString() == "false")
                {
                    isEnaled = false;
                }
                _searchCriteria.IsEnabled = isEnaled;

                return _searchCriteria;
            }
        }

        public RolesSearchCriteria RolesSearchCriteria
        {
            get
            {
                if (_rolesSearchCriteria == null)
                {
                    _rolesSearchCriteria = new RolesSearchCriteria();
                }
                return _rolesSearchCriteria;
            }
        }

        public void FillRolesList(EntitySearchResult<Role> entitySearchResult)
        {
            this.uxRolesListSource.DataSource = entitySearchResult;
        }

        protected void roleSearch_OnTextChanged(object sender, EventArgs e)
        {
            // just for testing
            uxName.Text = "text is changed!";

            RolesSearchCriteria.Name = uxRole.Text;
            this.Presenter.OnLoadRolesList();
        }

        protected void uxCancel_Click(object sender, EventArgs e)
        {
            this.uxPopup.Hide();
        }

        public void FillUsers(EntitySearchResult<UserDto> users)
        {
            this.uxUsersListSource.DataSource = users;
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            this.Presenter.OnSearchClick();
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            this.uxPopup.Show();
        }

        protected void fullName_Click(object sender, EventArgs e)
        {
            this.uxPopup.Show();
        }

        protected void uxRolesList_Selecting(object sender, ObjectContainerDataSourceSelectingEventArgs e)
        {
            this.Presenter.OnLoadRolesList();
        }

        protected void uxUsersList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Deleting")
            {
                this.Presenter.OnDeleteCommand(e.CommandArgument.ConvertTo(0));
            }
        }

        
    }
}

